package com.delong.lease.web.app.service.impl;

import com.delong.lease.common.exception.LeaseException;
import com.delong.lease.common.result.ResultCodeEnum;
import com.delong.lease.model.entity.ApartmentInfo;
import com.delong.lease.model.entity.FacilityInfo;
import com.delong.lease.model.entity.GraphInfo;
import com.delong.lease.model.entity.LabelInfo;
import com.delong.lease.model.enums.ItemType;
import com.delong.lease.web.app.mapper.*;
import com.delong.lease.web.app.service.ApartmentInfoService;
import com.delong.lease.web.app.vo.agreement.AgreementItemVo;
import com.delong.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.delong.lease.web.app.vo.apartment.ApartmentItemVo;
import com.delong.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Override
    public ApartmentItemVo selectApartmentInfoById(Long apartmentId) {
        //1、查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(apartmentId);
        //2、查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoById(apartmentId);
        //3、查询图片信息
        List<GraphInfo> graphInfoList = graphInfoMapper.selectGraphInfoById(ItemType.APARTMENT,apartmentId);
        //4、查询最小租金值
        BigDecimal minRent = roomInfoMapper.selectRoomMinRentById(apartmentId);

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setMinRent(minRent);
        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //1.查公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(id);
        if (apartmentInfo == null){
            throw new LeaseException(ResultCodeEnum.DATA_ERROR);
        }
        //2、查图片
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoById(ItemType.APARTMENT, id);
        //3、查标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoById(id);
        //4、查配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoByApartId(id);
        //5、租金最小值
        BigDecimal roomMinRent = roomInfoMapper.selectRoomMinRentById(id);
        //5、公寓是否被删除
        Boolean isDeleted = apartmentInfo.getIsDeleted() == 1;
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setMinRent(roomMinRent);
        apartmentDetailVo.setIsDelete(isDeleted);
        return apartmentDetailVo;
    }

}




